package com.qtz.demo.service;


import com.qtz.demo.bean.User;
import com.qtz.demo.mapper.UserMapper;
import com.qtz.demo.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public User login(User user) {
        User list = userMapper.login(user);
        return list;
    }

    @Override
    public User findByUsername(String username) {
        String str = "select * from users where username = " +username;
        User user = (User)redisUtil.get(str);
        if(null==user) {
            user = userMapper.findByUsername(username);
            redisUtil.set(str,user);
        }
        return user;
    }

    @Override
    public boolean addUser(User user) {

        String username=user.getUsername();
        User list=userMapper.findByUsername(username);//判断是否已存在
        if(list==null) {
            userMapper.addUser(user);
            String str1 = "select * from users";
            redisUtil.del(str1);
            return true;
        }
        return false;
    }

    @Override
    public List<User> showUnchecked() {
        return userMapper.showUnchecked();
    }

    @Override
    public boolean check(String uid) {
        return userMapper.check(uid);
    }
}
